<template>
  <div :class="[{'app-container': !this.$route.query.appName}]">
    <div class="filter-container">
      <el-select
        v-model="listQuery.appName"
        placeholder="服务名称"
        filterable
        clearable
        style="width: 400px"
        class="filter-item"
        size="small"
        :disabled="mode === 'single'"
        @keyup.enter.native="handleFilter"
      >
        <el-option
          v-for="item in appOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="listQuery.eurekaStatus"
        placeholder="实例状态"
        filterable
        clearable
        style="width: 200px;"
        class="filter-item"
        size="small"
        @keyup.enter.native="handleFilter"
      >
        <el-option
          v-for="item in eurekaStatusOptions"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-input
        v-model="listQuery.ip"
        placeholder="实例 IP"
        clearable
        style="width: 200px;"
        class="filter-item"
        size="small"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        size="small"
        @click="handleFilter"
      >
        查询
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="服务名称" min-width="240">
        <template slot-scope="scope">
          {{ scope.row.appName }}
        </template>
      </el-table-column>
      <el-table-column label="实例 ID" width="250">
        <template slot-scope="scope">
          {{ scope.row.instanceId }}
        </template>
      </el-table-column>
      <el-table-column label="服务状态" width="148">
        <template slot-scope="scope">
          <el-tag :type="eurekaStatusFilter(scope.row.eurekaStatus)">
            {{ scope.row.eurekaStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="IP" width="120">
        <template slot-scope="scope">
          {{ scope.row.ip }}
        </template>
      </el-table-column>
      <el-table-column label="端口" width="80">
        <template slot-scope="scope">
          {{ scope.row.port }}
        </template>
      </el-table-column>
      <el-table-column label="心跳时间" width="160">
        <template slot-scope="scope">
          {{ formatTime(scope.row.lastHeartbeatTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="80"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{ row }">
          <el-button
            v-if="row.eurekaStatus === 'OUT_OF_SERVICE'"
            type="text"
            size="mini"
            @click="handleUp(row)"
          >
            上线
          </el-button>
          <el-button
            v-if="row.eurekaStatus === 'UP'"
            type="text"
            size="mini"
            class="btn-cancel"
            @click="handleOutOfService(row)"
          >
            下线
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="fetchData"
    />
  </div>
</template>

<script>
import { getList } from '@/api/instance'
import { online, offline } from '@/api/app'
import Pagination from '@/components/Pagination'
import { listAppNames } from '@/api/app'
import waves from '@/directive/waves'

export default {
  // name: '服务列表',
  components: { Pagination },
  directives: { waves },
  props: {
    mode: {
      type: String,
      default: 'all'
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        appName: this.$route.query.appName,
        eurekaStatus: undefined
      },
      appOptions: [],
      eurekaStatusOptions: ['UP', 'OUT_OF_SERVICE', 'DOWN', 'STARTING', 'UNKNOWN']
    }
  },
  created() {
    this.fetchApps()
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((response) => {
        this.total = response.data.total
        this.list = response.data.items
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    fetchApps() {
      listAppNames().then((response) => {
        this.appOptions = response.data
      })
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.fetchData()
    },
    doUp(row) {
      var upParam = {
        appId: row.appName,
        instanceId: row.instanceId
      }
      online(upParam).then((response) => {})
    },
    doOutOfService(row) {
      var param = {
        appId: row.appName,
        instanceId: row.instanceId
      }
      offline(param).then((response) => {})
    },
    handleUp(row) {
      const confirmText = ['确认上线实例：', row.instanceId]
      const newDatas = []
      const h = this.$createElement
      for (const i in confirmText) {
        newDatas.push(h('p', null, confirmText[ i ]))
      }
      this.$confirm('', '提示', {
        message: h('div', null, newDatas),
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doUp(row)
      })
    },
    handleOutOfService(row) {
      const confirmText = ['确认下线实例：', row.instanceId]
      const newDatas = []
      const h = this.$createElement
      for (const i in confirmText) {
        newDatas.push(h('p', null, confirmText[ i ]))
      }
      this.$confirm('', '提示', {
        message: h('div', null, newDatas),
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doOutOfService(row)
      })
    },
    eurekaStatusFilter(status) {
      const statusMap = {
        UP: 'success',
        OUT_OF_SERVICE: 'warning',
        DOWN: 'danger'
      }
      return statusMap[status]
    },
    formatTime(timestamp) {
      if (timestamp && timestamp > 0) {
        return new Date(
          parseInt(timestamp)
        ).toLocaleString()
      }
      return timestamp
    }
  }
}
</script>
